home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / daoclass / cdaocomm.cls next >
Encoding:
Text File  |  1997-02-11  |  6.1 KB  |  195 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = 0   'False
  4. END
  5. Attribute VB_Name = "cDAOCommon"
  6. Attribute VB_Creatable = True
  7. Attribute VB_Exposed = True
  8. '|=============================================|
  9. '|     Class   : cDAOCommon                    |                                             |
  10. '|     Purpose : This Class Contains Common    |
  11. '|               DAO calls based on EasyDAO    |
  12. '|     Date    : 2/8/97                        |
  13. '|     Author  : Lee D. Johnson                |
  14. '|               jb32@ix.netcom.com            |
  15. '|               Lee@techknow-inc.com          |
  16. '|               pitty on IRC                  |
  17. '|     Version : 1.1                           |
  18. '|=============================================|
  19. '|=============================================|
  20. '|   NOTE :                                    |
  21. '| to use this class you must first Dim the    |
  22. '|  class in either a Module or Forms          |
  23. '|  Declarations. To do this use something     |
  24. '|  like this :                                |
  25. '|  Public ClsDAO As New cDAOCommon            |
  26. '|  Before You use this Class you must now set |
  27. '|  the name of your Data Control to the Class |
  28. '|  this is done like this :                   |
  29. '|  ClsDAO.SetDao data1                        |
  30. '|=============================================|
  31. Option Explicit
  32. '|=============================================|
  33. '|                                             |
  34. '|             Private Variables               |
  35. '|                                             |
  36. '|=============================================|
  37. 'Name for the Data Control which the
  38. 'User must initialize
  39. Private pstrDataSource As Data
  40. 'Tells if a Data Control is passed to it
  41. Private bDataNameSet As Boolean
  42. 'Tells the Class if It Has Authority to SaveNew
  43. 'Or the CancelNew
  44. Private bSavingData As Boolean
  45. 'Sets Authority for Find Next
  46. Private bFindNext As Boolean
  47. 'Declares for Message Boxes
  48. Private Msg
  49. Private Style
  50. Private Title
  51. Private Response
  52.  
  53.  
  54. Public Sub AddNewData()
  55. 'See if Data Control is set
  56. IsDataSet
  57. 'Add New Record Ensuring the record is at
  58. 'the end of the table
  59. pstrDataSource.Recordset.MoveLast
  60. pstrDataSource.Recordset.AddNew
  61. 'Disable the data control while new record
  62. pstrDataSource.Enabled = False
  63. 'Setting Allows CancelNew and AddNew to work
  64. bSavingData = True
  65. End Sub
  66.  
  67. Public Sub DeleteRecord()
  68. 'Check to see if the Data Control has been set
  69. IsDataSet
  70. 'Check to see that there are at least 3 records
  71. If pstrDataSource.Recordset.RecordCount < 3 Then
  72.     'Refuse Deletion for last 2 records
  73.     Msg = "You Cannot Delete the last 2 records"
  74.     MsgBox Msg
  75.     Exit Sub
  76.     End If
  77. 'Ask the User if they are sure they want to delete
  78. Msg = "You are about to permanetly remove this Record"
  79. Title = "Delete Record"
  80. Style = vbOKCancel + vbExclamation
  81. Response = MsgBox(Msg, Style, Title)
  82. If Response = vbCancel Then Exit Sub
  83. 'If User says yes then delete the record
  84. pstrDataSource.Recordset.Delete
  85. pstrDataSource.Recordset.MoveFirst
  86. End Sub
  87.  
  88. Public Function FindData(strField As String, strCriteria As String) As Boolean
  89. 'strField is the field that the data is stored in
  90. 'strCriteria is the Criteria asked
  91. 'Check to see if the Data Control has been set
  92. IsDataSet
  93. 'Searches For the first Instance depending on the criteria
  94. pstrDataSource.Recordset.FindFirst strField & "= '" & strCriteria & "'"
  95. 'If there is no match Tell user and set flags
  96. If pstrDataSource.Recordset.NoMatch Then
  97.    MsgBox "Record Not Found"
  98.    FindData = False
  99.    bFindNext = False
  100.    Else
  101.    'Found Criteria and setting FindNext Flags
  102.    FindData = True
  103.    bFindNext = True
  104.    End If
  105. End Function
  106.  
  107. Public Function FindNextData(strField As String, strCriteria As String) As Boolean
  108. 'Check to see if the Data Control has been set
  109. IsDataSet
  110. 'Tell User they need to use FindFirst First
  111. If bFindNext = False Then
  112.     MsgBox ("You Cannot Use FindNext unless you use FindFirst First")
  113.     Exit Function
  114.     End If
  115. 'Search the data object for the criteria
  116. pstrDataSource.Recordset.FindNext strField & "= '" & strCriteria & "'"
  117. 'Tell user no match and set Flags
  118. If pstrDataSource.Recordset.NoMatch Then
  119.     MsgBox "Record Not Found"
  120.     FindNextData = False
  121.     bFindNext = False
  122.     Else
  123.     'Set FindNext Flags
  124.     FindNextData = True
  125.     bFindNext = True
  126.     End If
  127. End Function
  128.  
  129. Private Sub IsDataSet()
  130. 'Check to see if the data control has been set
  131. 'If the Data control is not set the programmer is told
  132. 'and the application ends
  133. If bDataNameSet = False Then
  134.     'If Data Control Name Has Not been Initializied
  135.     MsgBox "You Must Use SetDAO before calling this"
  136.     End
  137.     End If
  138. 'Note This should never happen on the users end
  139. 'It occurs if the object was itialized without providing
  140. 'a data object to pass. If this error happened please see
  141. 'references in Public Sub SetDao(DataSource As Object)
  142. End Sub
  143.  
  144. Public Sub SaveNewData()
  145. 'Check to see if the Data Control has been set
  146. IsDataSet
  147. If bSavingData = False Then Exit Sub
  148. bSavingData = False
  149. 'Check to see if Data Control has been set
  150. IsDataSet
  151. 'Set up simple Error Handler
  152. On Error GoTo Datasave
  153. 'Save the data by moving to the first record
  154. 'and refreshing the data
  155. pstrDataSource.Enabled = True
  156. pstrDataSource.Recordset.MoveFirst
  157. pstrDataSource.Refresh
  158. Exit Sub
  159. 'Tell User there was an error saving
  160. Datasave:
  161. MsgBox "There was an Error saving the record : " & Chr(13) & Err.Description
  162. bSavingData = True
  163. End Sub
  164.  
  165. Public Sub CancelNewData()
  166. 'Cancel Add New
  167. 'Check to see if the Data Control has been set
  168. IsDataSet
  169. 'Ensure Class has the Authority to run this
  170. If Not bSavingData Then Exit Sub
  171. 'Refresh the data, hereby removing what the
  172. 'Users had in thier textboxes
  173. pstrDataSource.Refresh
  174. 'Disable Authority
  175. bSavingData = False
  176. 'Enable Data Object for user input
  177. pstrDataSource.Enabled = True
  178. End Sub
  179.  
  180.  
  181. Public Sub SetDao(DataSource As Object)
  182. 'Set's the Data Controls Name
  183. 'This is a must before using other calls
  184. Set pstrDataSource = DataSource
  185. bDataNameSet = True
  186. End Sub
  187.  
  188. Private Sub Class_Initialize()
  189. bSavingData = False
  190. bFindNext = False
  191. bDataNameSet = False
  192. End Sub
  193.  
  194.  
  195.